using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.RequestDto;
using TMS.Domain.Entity;

namespace TMS.Api.Controllers
{
    [ApiController]

    public class ClassAssignment : ControllerBase
    {//辅导员班级分配
        private readonly IRepository<Students> _Students;
        private readonly IRepository<College> _College;//学院
        private readonly IRepository<Major> _Major;//专业
        private readonly IRepository<Class> _Class;//班级

        public ClassAssignment(IRepository<Major> Major, IRepository<College> College, IRepository<Students> Students, IRepository<Class> Class)
        {
            _College = College;
            _Major = Major;
            _Students = Students;
            _Class = Class;
        }
        [HttpGet("/GetClassOf")]
        public object GetClassOf()
        {
            //学院(名称)  专业(名称)  班级(id，名称)  人数
            var Students = _Students.Table.ToList();

            var College = _College.Table.ToList();
            var Major = _Major.Table.ToList();
            var ClassInfo = _Class.Table.ToList();//通过班级表的majorId查询专业
            //需要一个对象数组 对象数组的长度为所有班级的多少
            object[] _ClassInfo = new object[ClassInfo.Count()];
            for (int i = 0; i < _ClassInfo.Length; i++)
            {
                //通过班级表的majorId查询专业表的专业名称和专业id
                var ClassId = ClassInfo[i].Id;//班级名称
                var ClassName = ClassInfo[i].ClassName;//班级名称

                //查询在这个班级同学的人数
                var count = Students.Where(x => x.ClassId == ClassId).Count();

                var _Majors = Major.Where(x => x.Id == ClassInfo[i].MajorId).FirstOrDefault();
                // var MajorId = _Majors.Id; //专业id
                var MajorName = _Majors.MajorName;//专业名称
                //通过专业表的CollegeId获取学院名称和学院id
                var _Colleges = College.FirstOrDefault(x => x.Id == _Majors.CollegeId);
                var CollegeName = _Colleges.CollegeName;//学院名称
                // var CollegeId = _Colleges.Id;//学院id
                _ClassInfo[i] = new
                {
                    CollegeName,
                    MajorName,
                    ClassName,
                    ClassId,
                    count
                };


            }
            // var MajorInfo =
            return new
            {
                code = 200,
                data = _ClassInfo,
                msg = "获取成功"
            };
        }

        [HttpGet("/GetClassInfo/{ClassId}")]
        public object GetClassInfo(Guid ClassId)
        {//获取某一个班级里所有学生信息//ClassId
            var students = _Students.Table.ToList();
            var ClassStudentInfo = students.Where(x => x.ClassId == ClassId).ToList();
            return new
            {
                code = 200,
                msg = "获取成功",
                data = ClassStudentInfo,


            };
        }

        [HttpPost("/AddStudentOfClass")]
        //往某一个班级添加学生
        public async Task<object> AddStudentOfClass(AddStudentOfClassDto AddStudentOfClassDto)
        {
            var classes = _Class.Table.ToList();
            var students = _Students.Table.ToList();
            //查看是否有该班级
            var IsClass = classes.FirstOrDefault(x => x.Id == AddStudentOfClassDto.ClassId);
            if (IsClass == null)
            {
                return new
                {
                    code = 400,
                    msg = "不存在该班级",
                    data = ""
                };
            }
            else
            {
                //根据学号查询是否有该学生
                var IsStudent = students.FirstOrDefault(x => x.StudentId == AddStudentOfClassDto.StudentId);
                if (IsStudent == null)
                {
                    return new
                    {
                        code = 400,
                        msg = "不存在该学生",
                        data = ""
                    };
                }
                else
                {
                    //判断是否存在其他班级
                    if (IsStudent.ClassId == new Guid("00000000-0000-0000-0000-000000000000"))
                    {//无班级状态
                        IsStudent.ClassId = AddStudentOfClassDto.ClassId;
                        await _Students.UpdateAsync(IsStudent);
                        return new
                        {
                            code = 200,
                            msg = "分配班级成功",
                            data = ""
                        };
                    }
                    else
                    {//考虑两种情况,一种是该同学的的班级存在，第二种是该同学的班级以解散
                        var ExistClass = classes.FirstOrDefault(x => x.Id == IsStudent.ClassId);
                        if (ExistClass == null)
                        {//该同学原来的班级已经不存在，进行班级的更新
                            IsStudent.ClassId = AddStudentOfClassDto.ClassId;
                            await _Students.UpdateAsync(IsStudent);
                            return new
                            {
                                code = 200,
                                msg = "分配班级成功",
                                data = ""
                            };
                        }
                        else
                        {//返回该同学已在的班级

                            return new
                            {
                                code = 400,
                                msg = $"该同学已在{ExistClass.ClassName}",
                                data = ""
                            };
                        }

                    }
                    // IsStudent.ClassId
                }
            }
            // return new { };
        }

        [HttpDelete("/DelStudentOfClass/{StudentId}")]
        public async Task<object> DelStudentOfClass(string StudentId)
        {
            var students = _Students.Table.ToList();
            var IsStudents = students.FirstOrDefault(x => x.StudentId == StudentId);
            if (IsStudents==null)
            {//不存在该学生
                return new
                {
                    code=400,
                    msg="该学生不存在",
                    data=""
                };
                
            }else
            {
                IsStudents.ClassId=new Guid("00000000-0000-0000-0000-000000000000");
                await _Students.UpdateAsync(IsStudents);
                return new
                {
                    code=200,
                    msg="移除成功",
                    data=""
                };
            }
        }
    }
}